package main

import (
	"fmt"
	"os"
	"time"
)

func CountDown(n int, countChan chan int, finishChan chan struct{}) {
	if n <= 0 {
		return
	}
	ticker := time.NewTicker(1 * time.Second)
	for {
		countChan <- n
		<-ticker.C
		n--
		if n <= 0 {
			ticker.Stop()
			finishChan <- struct{}{}
			break
		}
	}
}

func Abort(abortChan chan struct{}) {
	bs := make([]byte, 1)
	os.Stdin.Read(bs)
	abortChan <- struct{}{}
}

/*
	10
	9
	8
	7
	6
	5
	4
	3
	2
	1
	go on!
*/
func main() {
	countChan := make(chan int)
	finishChan := make(chan struct{})
	abortChan := make(chan struct{})

	go CountDown(10, countChan, finishChan)
	go Abort(abortChan)

loop:
	for {
		select {
		case n := <-countChan:
			fmt.Println(n)
		case <-finishChan:
			fmt.Println("go on!")
			break loop
		case <-abortChan:
			fmt.Println("abort!")
			break loop
		}
	}
}
